/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.gui.component;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

import edu.enac.geoapp.gui.custom.component.ImageButton;
import edu.enac.geoapp.gui.listener.DataScrollListener;

@SuppressWarnings("serial")
/**
 * Component that allows to navigate among the various bundles of data
 * contained in a GML file
 */
public class ImageScroller extends JComponent {
	private ImageButton nextDateButton;
	private ImageButton previouDateButton;
	// private ImageButton nextIsoLevel;
	// private ImageButton previousIsoLevel;

	private List<DataScrollListener> dataScrollListeners;

	/**
	 * Constructor initializing the various buttons 
	 */
	public ImageScroller() {
		dataScrollListeners = new ArrayList<DataScrollListener>();

		// gui
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		// // up button
		// nextIsoLevel = new ImageButton("images/up_button.png",
		// "images/up_button.png");
		// nextIsoLevel.setToolTipText("Next isobaric level");
		// nextIsoLevel.addMouseListener(new MouseAdapter() {
		// @Override
		// public void mouseReleased(MouseEvent e) {
		// for (DataScrollListener listener : dataScrollListeners) {
		// listener.nextIsoLevel();
		// }
		// }
		// });

		// // down button
		// previousIsoLevel = new ImageButton("images/down_button.png",
		// "images/down_button.png");
		// previousIsoLevel.setToolTipText("Previous isobaric level");
		// previousIsoLevel.addMouseListener(new MouseAdapter() {
		// @Override
		// public void mouseReleased(MouseEvent e) {
		// for (DataScrollListener listener : dataScrollListeners) {
		// listener.previousIsoLevel();
		// }
		// }
		// });
		//
		
		// nextDate button
		nextDateButton = new ImageButton("images/next_button.png",
				"images/next_button.png");
		nextDateButton.setRolloverEnabled(true);
		nextDateButton.setRolloverIcon(new ImageIcon(
				"images/next_button_rollover.png"));
		nextDateButton.setDisabledIcon(new ImageIcon(
				"images/next_button_inactive.png"));

		nextDateButton.setToolTipText("Next prevision");
		nextDateButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (nextDateButton.isEnabled())
					for (DataScrollListener listener : dataScrollListeners) {
						listener.nextDate();
					}
			}
		});
		nextDateButton.setActive(true);

		// previousDate button
		previouDateButton = new ImageButton("images/previous_button.png",
				"images/previous_button.png");
		previouDateButton.setDisabledIcon(new ImageIcon(
				"images/previous_button_inactive.png"));
		previouDateButton.setRolloverEnabled(true);
		previouDateButton.setRolloverIcon(new ImageIcon(
				"images/previous_button_rollover.png"));
		previouDateButton.setToolTipText("Previous prevision");
		previouDateButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (previouDateButton.isEnabled())
					for (DataScrollListener listener : dataScrollListeners) {
						listener.previousDate();
					}
			}
		});
		previouDateButton.setActive(true);

		// disposition
		add(previouDateButton);
		add(Box.createRigidArea(new Dimension(5, 0)));
		add(nextDateButton);
		// nextIsoLevel.setAlignmentX(Component.CENTER_ALIGNMENT);
		// add(nextIsoLevel);
		// add(Box.createRigidArea(new Dimension(0,3)));
		// add(new Container());
		// add(Box.createRigidArea(new Dimension(0,3)));
		// previousIsoLevel.setAlignmentX(Component.CENTER_ALIGNMENT);
		// add(previousIsoLevel);

	}

	public void addScrollerListener(DataScrollListener listener) {
		dataScrollListeners.add(listener);
	}

	public void removeScrollerListener(DataScrollListener listener) {
		dataScrollListeners.remove(listener);
	}

	public void setEnabledNextButton(boolean enabled) {
		nextDateButton.setEnabled(enabled);
	}

	public void setEnabledPreviousButton(boolean enabled) {
		previouDateButton.setEnabled(enabled);
	}
}
